package pri.damai.xiaowu.usercenter.api.web;


import io.swagger.annotations.Api;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import pri.damai.xiaowu.common.core.form.IdForm;
import pri.damai.xiaowu.common.core.vo.ResultVO;
import pri.damai.xiaowu.common.log.anno.XiaoWuLog;
import pri.damai.xiaowu.goods.client.SkuService;
import pri.damai.xiaowu.goods.common.command.SkuCreateCommand;
import pri.damai.xiaowu.usercenter.common.dto.UserDTO;
import pri.damai.xiaowu.usercenter.sysuser.application.UserAppService;
import pri.damai.xiaowu.usercenter.sysuser.command.AddBaseUserCommand;
import pri.damai.xiaowu.usercenter.sysuser.entity.SysUser;
import pri.damai.xiaowu.usercenter.sysuser.service.impl.SysUserServiceImpl;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static pri.damai.xiaowu.common.core.vo.ResultVO.success;

/**
 * 中心账户表(SysUser)表控制层
 * @Desc
 * @Author DaMai
 * @Date 2022-03-03 17:54:04
 */
@Api(tags = "用户账户")
@XiaoWuLog
@RestController
@RequestMapping("user")
public class SysUserController {
    /**
     * 服务对象
     */
    @Resource
    private SysUserServiceImpl sysUserService;
    @Resource
    UserAppService userAppService;


    /**
     * 通过主键查询单条数据
     * @return 单条数据
     */
    @PostMapping("/getUserById")
    public ResultVO<SysUser> selectOne(@RequestBody @Valid IdForm idForm) {

        SysUser sysUser = this.sysUserService.getById(idForm.getId());

        return success(sysUser);
    }

    /**
     * 新增数据
     *
     * @param addBaseUserCommand 实体对象
     * @return 新增结果
     */
    @PostMapping("/createBaseUser")
    public ResultVO<UserDTO> insert(@RequestBody AddBaseUserCommand addBaseUserCommand) {
        UserDTO baseUser = this.userAppService.createBaseUser(addBaseUserCommand);
        return success(baseUser);
    }

    /**
     * 修改数据
     *
     * @param sysUser 实体对象
     * @return 修改结果
     */
    @PostMapping("/updateUser")
    public ResultVO<Boolean> update(@RequestBody SysUser sysUser) {
        return success(this.sysUserService.updateById(sysUser));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @PostMapping("/delByIds")
    public ResultVO<Boolean> delete(@RequestParam("idList") List<Integer> idList) {
        return success(this.sysUserService.removeByIds(idList));
    }

    @DubboReference
    SkuService skuService;

    @PostMapping("/test_global_transactional")
//    @GlobalTransactional(rollbackFor = Exception.class)
    public ResultVO<Boolean> delete(@RequestBody SkuCreateCommand skuCreateCommand) {
        SysUser sysUser = sysUserService.getById(1);
        sysUser.setUsername("update_1");
        sysUserService.saveOrUpdate(sysUser);
        skuService.createSku(skuCreateCommand);
        throw new RuntimeException();
        //return success();
    }

}
